package com.zhou.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @PostMapping("/login")
    public String login(HttpSession session, String username, String password, Map<String, Object> map){
        System.out.println(username+"----"+password);
        if (!StringUtils.isEmpty(username) && "123".equals(password)){
            session.setAttribute("loginUser", username);
            return "redirect:/index";
        } else {
            map.put("msg", "用户名或密码错误");
            return "main/login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        Object loginUser = session.getAttribute("loginUser");
        System.out.println(loginUser+"退出登录");
        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:/";
    }
}
